/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2008-2011, Salzburg Research. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * - Neither the name of the LMF library client Project nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributor(s):
 * Stephanie Stroka
 *
 * File Info:
 * User: Stephanie Stroka
 * Date: 2011/08/19
 * Time: 18:04
 *
 * Project: lmflib
 */

package kiwi.facading.services;

import kiwi.facading.api.LiteralService;
import kiwi.facading.model.Constants;
import kiwi.facading.model.resources.KiWiDoubleLiteral;
import kiwi.facading.model.resources.KiWiIntLiteral;
import kiwi.facading.model.resources.KiWiLiteral;
import kiwi.facading.model.resources.KiWiStringLiteral;
import kiwi.facading.utils.KiWiFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.Locale;

/**
 * Add file description here!
 * <p/>
 * User: sschaffe
 */
public class LiteralServiceImpl implements LiteralService {

    private Logger log =  LoggerFactory.getLogger(this.getClass());


//    @KiWiCache("literal-cache")
//    private Cache literalNodeCache;


    /**
     * Create a new KiWiLiteral of the provided type.
     *
     * TODO: caching
     *
     * @param <T>
     * @param value
     * @param language
     * @param type
     * @return
     */
    public synchronized <T> KiWiLiteral createLiteral(T value, Locale language, String type) {
        if(value != null) {
            if(type == null) {
                type = getXSDType(value.getClass());
            }

            KiWiLiteral result;
//                TODO: String cacheKey = TripleStoreUtil.createLiteralCacheKey(MD5.md5sum(value.toString()),language,type);

            // create a new literal depending on type
            if(Date.class.equals(value.getClass())) {
                result = new KiWiStringLiteral(KiWiFormatUtils.ISO8601FORMAT.format(value), language, type);
            } else if(Integer.class.equals(value.getClass()) || int.class.equals(value.getClass())) {
                result = new KiWiIntLiteral(((Integer)value).longValue(), language, type);
            } else if(Long.class.equals(value.getClass()) || long.class.equals(value.getClass())) {
                result = new KiWiIntLiteral((Long)value, language, type);
            } else if(Double.class.equals(value.getClass()) || double.class.equals(value.getClass())) {
                result = new KiWiDoubleLiteral((Double)value, language, type);
            } else if(Float.class.equals(value.getClass()) || float.class.equals(value.getClass())) {
                result = new KiWiDoubleLiteral(((Float)value).doubleValue(), language, type);
            } else {
                result = new KiWiStringLiteral(value.toString(), language, type);
            }

//                literalNodeCache.put(new Element(cacheKey,result));


            return result;
        } else {
            return null;
        }
    }



    /**
     * Create a new KiWiLiteral. The type is inferred by Java reflection.
     * @param <T>
     * @param value
     * @return
     */
    public <T> KiWiLiteral createLiteral(T value) {
    	return createLiteral(value, null, getXSDType(value.getClass()));
    }

    /**
     * Create a new KiWiLiteral. The type is inferred by Java reflection.
     * @param <T>
     * @param value
     * @return
     */
    public <T> KiWiLiteral createLiteral(T value, Locale langauge) {
    	return createLiteral(value, langauge, getXSDType(value.getClass()));
    }


    /**
      * Return the appropriate XSD type for RDF literals for the provided Java class.
      * @param javaClass
      * @return
      */
     private String getXSDType(Class javaClass) {
         if(String.class.isAssignableFrom(javaClass)) {
             return Constants.NS_XSD+"string";
         } else if(Integer.class.isAssignableFrom(javaClass)) {
             return Constants.NS_XSD+"integer";
         } else if(Long.class.isAssignableFrom(javaClass)) {
             return Constants.NS_XSD+"long";
         } else if(Double.class.isAssignableFrom(javaClass)) {
             return Constants.NS_XSD+"double";
         } else if(Float.class.isAssignableFrom(javaClass)) {
             return Constants.NS_XSD+"float";
         } else if(Date.class.isAssignableFrom(javaClass)) {
             return Constants.NS_XSD+"dateTime";
         } else if(Boolean.class.isAssignableFrom(javaClass)) {
             return Constants.NS_XSD+"boolean";
         } else {
             return Constants.NS_XSD+"string";
         }
     }

    /**
     * Clear all caches that are currently active; used e.g. in case of a rollback to avoid
     * detached triples that are not really persisted.
     */
    public void clearCaches() {
//        literalNodeCache.removeAll();
     }


}